﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XStudio.XCRF.Data.Entities;
using System.Linq.Expressions;
using System.Data.Entity;

namespace XStudio.XCRF.Data.Business
{
    public static class BusinessExtension
    {
        /// <summary>
        /// 从所有记录中筛选主记录
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IQueryable<T> GetMasterRecords<T>(this IQueryable<T> entities) where T : BaseEntity<T>
        {
            return entities.Where(entity => !entity.IsDeleted);
        }
        /// <summary>
        /// 从所有记录中筛选主记录
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IEnumerable<T> GetMasterRecords<T>(this IEnumerable<T> entities) where T : BaseEntity<T>
        {
            return entities.Where(entity => !entity.IsDeleted);
        }
        /// <summary>
        /// 从所有记录中筛选已删除的记录
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IQueryable<T> GetDeletedRecords<T>(this IQueryable<T> entities) where T : BaseEntity<T>
        {
            return entities.Where(entity => entity.IsDeleted && entity.MasterID != null);
        }
        /// <summary>
        /// 从所有记录中筛选已删除的记录
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IEnumerable<T> GetDeletedRecords<T>(this IEnumerable<T> entities) where T : BaseEntity<T>
        {
            return entities.Where(entity => entity.IsDeleted && entity.MasterID != null);
        }

        /// <summary>
        /// 查找满足指定条件的实体集合
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities"></param>
        /// <param name="condition">条件表达式</param>
        /// <returns></returns>
        public static IQueryable<T> Find<T>(this IQueryable<T> entities,
            Expression<Func<T, bool>> condition) where T : BaseEntity<T>
        {
            return entities.GetMasterRecords().Where(condition);
        }
        /// <summary>
        /// 查找指定ID的实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities"></param>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public static T Find<T>(this IQueryable<T> entities, Guid id) where T : BaseEntity<T>
        {
            return entities.Find(e => e.ID == id).First();
        }
        /// <summary>
        /// 判断指定条件的实体是否存在
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities"></param>
        /// <param name="condition">条件表达式</param>
        /// <returns></returns>
        public static bool Exist<T>(this IQueryable<T> entities,
            Expression<Func<T, bool>> condition) where T : BaseEntity<T>
        {
            return entities.Find(condition).Any();
        }

        /// <summary>
        /// 判断指定ID的实体是否存在
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities"></param>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public static bool Exist<T>(this IQueryable<T> entities, Guid id) where T : BaseEntity<T>
        {
            return entities.Exist(e => e.ID == id);
        }
    }
}
